// src/components/FormFields.jsx
import React from 'react';
import { Input, Form, Picker, ImageUploader } from 'antd-mobile';
import { motion } from 'framer-motion';

// Email field with validation
export const EmailField = ({ name = "email", required = true }) => {
  return (
    <Form.Item
      name={name}
      label="邮箱"
      rules={[
        { required, message: '请输入邮箱' },
        { type: 'email', message: '请输入有效的邮箱地址' }
      ]}
    >
      <Input placeholder="请输入邮箱" />
    </Form.Item>
  );
};

// Password field with validation
export const PasswordField = ({ name = "password", required = true }) => {
  return (
    <Form.Item
      name={name}
      label="密码"
      rules={[
        { required, message: '请输入密码' },
        { min: 6, message: '密码至少需要6位字符' }
      ]}
    >
      <Input placeholder="请输入密码" type="password" />
    </Form.Item>
  );
};

// Username field with validation
export const UsernameField = ({ name = "username", required = true }) => {
  return (
    <Form.Item
      name={name}
      label="用户名"
      rules={[
        { required, message: '请输入用户名' },
        { 
          pattern: /^[a-zA-Z0-9_\u4e00-\u9fa5]+$/,
          message: '用户名只能包含字母、数字、下划线和中文字符'
        }
      ]}
    >
      <Input placeholder="请输入用户名" />
    </Form.Item>
  );
};

// City picker field
export const CityPickerField = ({ 
  name = "city", 
  cities = [], 
  visible = false, 
  setVisible = () => {},
  required = true 
}) => {
  return (
    <Form.Item
      name={name}
      label="所在城市"
      onClick={() => setVisible(true)}
      trigger="onConfirm"
      rules={[{ required, message: '请选择城市' }]}
      arrow
    >
      <Picker
        columns={cities}
        visible={visible}
        onClose={() => setVisible(false)}
      >
        {values => values.length > 0 ? values[0]?.label : '请选择城市'}
      </Picker>
    </Form.Item>
  );
};

// Avatar uploader field
export const AvatarUploaderField = ({
  name = "avatar",
  fileList = [],
  setFileList = () => {},
  uploadFn = () => {},
  required = true
}) => {
  return (
    <Form.Item
      name={name}
      label="头像"
      rules={[{ required, message: '请上传头像' }]}
    >
      <ImageUploader
        value={fileList}
        onChange={setFileList}
        upload={uploadFn}
        maxCount={1}
        showUpload={fileList.length < 1}
      />
    </Form.Item>
  );
};

export default {
  EmailField,
  PasswordField,
  UsernameField,
  CityPickerField,
  AvatarUploaderField
};